

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextField;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/*
 * Jared Piimauna
 * ICS 314 Calender GUI
 */

public class ICS314CalenderGUI extends javax.swing.JPanel implements ActionListener, FocusListener{


    /*all the buttons, fields, labels*/                   
   
    private final JLabel statusMessage = new JLabel();
    private final JTextField descriptionField = new JTextField();
    private final JTextField locationField = new JTextField();
    private final JTextField eventField = new JTextField();
    private final JButton generateButton = new JButton();
    private final JButton clearAllButton = new JButton();
    private final JLabel eventLabel = new JLabel();
    private final JLabel descriptionLabel = new JLabel();;
    private final JLabel endTimeLabel = new JLabel();
    private final JLabel locationLabel = new JLabel();
    private final JLabel startTimeLabel = new JLabel();
    private final JLabel startTimeDateLabel = new JLabel();
    private final JLabel endTimeDateLabel = new JLabel();
    private final JLabel startTimeTimeLabel = new JLabel();
    private final JLabel endTimeTimeLabel = new JLabel();
    private final JFormattedTextField startTimeDateField = new JFormattedTextField();
    private final JFormattedTextField endTimeDateField = new JFormattedTextField();
    private final JFormattedTextField startTimeTimeField = new JFormattedTextField();
    private final JFormattedTextField endTimeTimeField = new JFormattedTextField();
    boolean initialize;
    
    /*Menu*/
    private final JMenu file = new JMenu();
    private final JMenu edit = new JMenu();
    private final static JMenuBar menuBar = new JMenuBar();
    
    
        public ICS314CalenderGUI() {
            initialize = initialize();
        }
        
        
                                 
        private boolean initialize() {
            try {
            /*add the labels, fields, buttons*/
            add(statusMessage);
            add(descriptionField);
            add(locationField);
            add(eventField);
            add(generateButton);
            add(clearAllButton);
            add(eventLabel);
            add(descriptionLabel);
            add(endTimeLabel);
            add(locationLabel);
            add(startTimeLabel);
            add(startTimeDateLabel);
            add(endTimeDateLabel);
            add(startTimeTimeLabel);
            add(endTimeTimeLabel);
            add(startTimeDateField);
            add(endTimeDateField);
            add(startTimeTimeField);
            add(endTimeTimeField);
            add(file);
            add(edit);
            add(menuBar);
            
            
            /*menubar*/
            file.setText("File");
            edit.setText("Edit");
            menuBar.add(file);
            menuBar.add(edit);
            
            
            
            /*set initial text*/
            statusMessage.setFont(new java.awt.Font("Verdana", 1, 12));
            statusMessage.setText("Welcome to team Cocoa's .ICS generator!");
            eventLabel.setText("Event");
            locationLabel.setText("Location");
            descriptionLabel.setText("Description");
            descriptionField.setText("Decribe the event");
            eventField.setText("What's happening?");
            locationField.setText("Where at?");
            startTimeLabel.setText("Start Time");
            endTimeLabel.setText("End Time");
            startTimeDateLabel.setText("Date");
            endTimeDateLabel.setText("Date");
            startTimeTimeLabel.setText("Time");
            endTimeTimeLabel.setText("Time");
            startTimeDateField.setText("MM/DD/YYYY");
            endTimeDateField.setText("MM/DD/YYYY");
            startTimeTimeField.setText("HH:MM:SS");
            endTimeTimeField.setText("HH:MM:SS");
            generateButton.setText("Generate!");
            clearAllButton.setText("Clear All");
            
            
            
            
            /*action listeners*/
            eventField.addActionListener(this);
            locationField.addActionListener(this);
            descriptionField.addActionListener(this);
            clearAllButton.addActionListener(this);
            generateButton.addActionListener(this);

            descriptionField.addFocusListener(this);
            locationField.addFocusListener(this);
            eventField.addFocusListener(this);
            startTimeDateField.addFocusListener(this);
            endTimeDateField.addFocusListener(this);
            startTimeTimeField.addFocusListener(this);
            endTimeTimeField.addFocusListener(this);
            /*layout for components*/

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(startTimeDateLabel)
                        .addComponent(startTimeTimeLabel))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(154, 154, 154)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(endTimeLabel)
                                .addComponent(generateButton))
                            .addGap(29, 29, 29)
                            .addComponent(clearAllButton))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(startTimeLabel)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(eventLabel)
                                                .addComponent(locationLabel)
                                                .addComponent(descriptionLabel))
                                            .addGap(30, 30, 30)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(eventField, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                                                .addComponent(locationField)
                                                .addComponent(descriptionField)))
                                        .addComponent(startTimeDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(startTimeTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                                            .addComponent(endTimeTimeLabel)
                                            .addGap(145, 145, 145))))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(158, 158, 158)
                                    .addComponent(endTimeDateLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(endTimeTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(endTimeDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGap(0, 0, Short.MAX_VALUE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(100, 100, 100)
                    .addComponent(statusMessage)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addComponent(statusMessage)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(eventLabel)
                        .addComponent(eventField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(7, 7, 7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(locationLabel)
                        .addComponent(locationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(descriptionLabel))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(startTimeLabel)
                        .addComponent(endTimeLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(startTimeDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(endTimeDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(startTimeDateLabel)
                        .addComponent(endTimeDateLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(startTimeTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(endTimeTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(startTimeTimeLabel)
                        .addComponent(endTimeTimeLabel))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(generateButton)
                        .addComponent(clearAllButton))
                    .addContainerGap())
            );
            }catch(Exception e){
                return false;
            }
            return true;
            
        }
        
        
        
public void focusGained(FocusEvent event) {
        if(event.getSource()==descriptionField){
            descriptionField.setText("");
        }
        if(event.getSource()==locationField){
            locationField.setText("");
        }
        if(event.getSource()==eventField){
            eventField.setText("");
        }
        if(event.getSource()==startTimeDateField){
            if(startTimeDateField.getText().equals("MM/DD/YYYY"))
                startTimeDateField.setText("");
        }
        if(event.getSource()==endTimeDateField){
            if(endTimeDateField.getText().equals("MM/DD/YYYY"))
                endTimeDateField.setText("");
        }
        if(event.getSource()==startTimeTimeField){
            if(startTimeTimeField.getText().equals("HH:MM:SS"))
                startTimeTimeField.setText("");        
        }
        if(event.getSource()==endTimeTimeField){
            if(endTimeTimeField.getText().equals("HH:MM:SS"))
                endTimeTimeField.setText("");
        }
    }
    public void highlight(Color color,String type){
        if(type.equals("date")){
            startTimeDateField.setBackground(color);
            endTimeDateField.setBackground(color);
        }else if(type.equals("time")){
            startTimeTimeField.setBackground(color);
            endTimeTimeField.setBackground(color);
        }
    }
    public void focusLost(FocusEvent event) {
        if(event.getSource()==startTimeDateField){
            Color status = (ICSFormat.valid("date",startTimeDateField.getText()).equals(""))?Color.YELLOW:Color.GREEN;
            startTimeDateField.setBackground(status);
        }
        if(event.getSource()==endTimeDateField){
            Color status = (ICSFormat.valid("date",endTimeDateField.getText()).equals(""))?Color.YELLOW:Color.GREEN;
            endTimeDateField.setBackground(status);
        }
        if(event.getSource()==startTimeTimeField){
            Color status = (ICSFormat.valid("time",startTimeTimeField.getText()).equals(""))?Color.YELLOW:Color.GREEN;
            startTimeTimeField.setBackground(status);
        }
        if(event.getSource()==endTimeTimeField){
            Color status = (ICSFormat.valid("time",endTimeTimeField.getText()).equals(""))?Color.YELLOW:Color.GREEN;
            endTimeTimeField.setBackground(status);        }
    }
    /* the actions to be performed for the buttons/etc. */
    public void actionPerformed(ActionEvent event) {
        try {

            // Clears all fields (convenient)
            if (event.getSource() == clearAllButton) {
                statusMessage.setText("All fields cleared!!");
                eventField.setText("");
                locationField.setText("");
                descriptionField.setText("");
                startTimeDateField.setText("");
                endTimeDateField.setText("");
                startTimeTimeField.setText("");
                endTimeTimeField.setText("");
                startTimeTimeField.setBackground(Color.WHITE);
                endTimeTimeField.setBackground(Color.WHITE);
                startTimeDateField.setBackground(Color.WHITE);
                endTimeDateField.setBackground(Color.WHITE);      
            }

            if (event.getSource() == generateButton) {
                
                //check that the times make contexual sense
                if(ICSFormat.compareDate(startTimeDateField.getText(),endTimeDateField.getText())==1){//if day is bigger
                    highlight(Color.GREEN,"date");
                    highlight(Color.GREEN,"time");                    
                }else if(ICSFormat.compareDate(startTimeDateField.getText(),endTimeDateField.getText())==0){ //if same day
                    if(ICSFormat.compareTime(startTimeTimeField.getText(),endTimeTimeField.getText())==-1){ //and lesser times
                        highlight(Color.GREEN,"date");
                        highlight(Color.YELLOW,"time");
                        statusMessage.setText("Check your start and end times");
                        return;
                    }
                }else{//day is smaller
                    highlight(Color.YELLOW,"date");
                    highlight(Color.GREEN,"time");      
                    statusMessage.setText("Check your start and end dates");
                    return;
                }
                
                // to be implemented. insert method(s) for parsing and gathering
                // input
                TimeZone tz = Calendar.getInstance().getTimeZone();
                FileMaker generator = new FileMaker();
                generator.set_attribute("VERSION", "2.0", "calendar");
                generator.set_attribute("CALSCALE", "GREGORIAN", "calendar");
                generator.set_attribute("SUMMARY", eventField.getText());
                generator.set_attribute("DESCRIPTION",descriptionField.getText());
                generator.set_attribute("DTSTAMP;TZID:" + tz.getID(),ICSFormat.timestamp());
                generator.set_attribute("DTSTART;TZID:" + tz.getID(), ICSFormat.valid("date",startTimeDateField.getText()) + ICSFormat.valid("time",startTimeTimeField.getText()));
                generator.set_attribute("DTEND;TZID:" + tz.getID(), ICSFormat.valid("date",endTimeDateField.getText()) + ICSFormat.valid("time",endTimeTimeField.getText()));   
                generator.set_attribute("LOCATION", locationField.getText());
                if(generator.generate(eventField.getText().replaceAll(" ","_"))){
                    statusMessage.setText(".ICS file generated! Check it out.");
                }else{
                    statusMessage.setText("Bad Input");                    
                }
            }
        } catch (Exception E) {
            statusMessage.setText("Something went wrong");
        }
    }
    
    public boolean getInitialize() {
        return initialize;
    }
        
        
          
                           
    
    
    /*test*/
    public static void main(String[] args) {
          
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("t3@m C0cO@ LOL");
        ICS314CalenderGUI panel = new ICS314CalenderGUI();
        window.add(panel);
        window.pack();
        window.setVisible(true);
        panel.setLayout(new BorderLayout());
        panel.add(menuBar, BorderLayout.NORTH);
    }
}


